<?php
	include_once '../config/config.php';
	session_start();
	session_unset();
	
	$login = $_POST['login'];
	$password = md5($_POST['password']);

	$query = "SELECT * FROM user WHERE login = '".$login."' AND password = '".$password."'";
	$select = $pdo->query($query);
	
	//Check if user exists 
	if($select->rowCount() == 1){
		while($row = $select->fetch()){
			$_SESSION['login'] = $row["login"];
		}
		$chatLogPath = "../coreChat/log/chatLog.txt";
		$data = "<font color='#0a0'>--- ".$_SESSION['login']." is online"." ---</font><br/>";
		file_put_contents($chatLogPath, $data, FILE_APPEND);
		//Generate random color for the current user
		$randomcolor = '#' . strtoupper(dechex(rand(0,10000000)));
		$_SESSION['loginColor'] = $randomcolor;
		header("Location: ../coreChat/chat.php");	
	}else{
		header("Location: login.php");
		$_SESSION['loginError'] = "Wrong login or password";
	}
	
?>